package com.lsw.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lsw.R;


/**
 * Created by lsw on 2017/7/7.
 * 自定义toast
 */

public class MyToast extends Toast {
    private Context context;
    private TextView textview;

    public MyToast(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public void init() {
        //加载Toast布局
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.mytoast, null);
        //初始化布局控件
        textview = (TextView) toastRoot.findViewById(R.id.textview);
//        //获取屏幕高度
//        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        int height = wm.getDefaultDisplay().getHeight();
//        //Toast的Y坐标是屏幕高度的1/3，不会出现不适配的问题
//        toastStart.setGravity(Gravity.TOP, 0, height / 3);
        this.setDuration(Toast.LENGTH_SHORT);
        this.setView(toastRoot);
    }

    public void Showgray(String msg) {
        textview.setBackgroundColor(context.getResources().getColor(R.color.gray));
        textview.setTextColor(context.getResources().getColor(R.color.black));
        textview.setText(msg);
        show();
    }

    public void Showred(String msg) {
        textview.setBackgroundColor(context.getResources().getColor(R.color.text_prompt));
        textview.setTextColor(context.getResources().getColor(R.color.white));
        textview.setText(msg);
        show();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void Showblack(String msg) {
        textview.setBackground(context.getDrawable(R.drawable.black_toast));
        textview.setTextColor(context.getResources().getColor(R.color.white));
        textview.setText(msg);
        show();
    }

    @Override
    public void setView(View view) {
        super.setView(view);
    }
}
